﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Service.System;
using Infrastructure.System;
using Domain.Entities;
using Presentation.Core;
namespace DDD从零起步.Controllers
{
    public class DepartmentController : BaseController
    {
        IDepartmentRepository iDepartmentRepository;
        public DepartmentController()
        {
            iDepartmentRepository = new DepartmentRepository();
        }

        //
        // GET: /Department/
        /// <summary>
        /// 部门列表
        /// </summary>
        /// <param name="deptSelect">为1渲染DeptSelect，为2渲染DeptSelectMore,否则渲染Index</param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult Index(int? deptSelect, int? page)
        {
            var model = iDepartmentRepository.GetEntities().ToList();
            if (deptSelect.HasValue && deptSelect.Value == 1)
                return View("DeptSelect", model);//渲染DeptSelect视图,单选功能
            else if (deptSelect.HasValue && deptSelect.Value == 2)
                return View("DeptSelectMore", model);//渲染DeptSelect视图,多选功能
            else
                return View(model);
        }

        //
        // GET: /Department/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                WebDepartments entity = new WebDepartments();
                TryUpdateModel(entity);
                entity.UpdateDate = DateTime.Now;
                entity.Operator = string.Empty;
                entity.Status = 100;
                iDepartmentRepository.Add(entity);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Department/Edit/5

        public ActionResult Edit(int id)
        {
            var model = iDepartmentRepository.Find(id);
            return View(model);
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                WebDepartments entity = iDepartmentRepository.Find(id);//取当前实体
                TryUpdateModel(entity);//从视频更新到模型
                entity.UpdateDate = DateTime.Now;//手动为模型赋值
                iDepartmentRepository.Modify(entity);//提交到数据库
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Department/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                iDepartmentRepository.Remove(new WebDepartments { DepartmentID = id });
                return RedirectArtDialogToAction("操作成功", "Index");
            }
            catch (Exception ex)
            {

                return RedirectArtDialogToAction("操作失败，信息为:" + ex.Message, "Index");
            }
        }


    }
}
